package com.apex.sdk.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;

import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityCircleBinding;


public class CycleActivity extends AppCompatActivity {
    private final int CYCLE_CODE = 0xFF;
    ActivityCircleBinding activityCircleBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityCircleBinding = ActivityCircleBinding.inflate(getLayoutInflater());
        setContentView(activityCircleBinding.getRoot());
        activityCircleBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityCircleBinding.tool);
        getSupportActionBar().setTitle(null);
        activityCircleBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        activityCircleBinding.onceCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    activityCircleBinding.sundayCheck.setChecked(false);
                    activityCircleBinding.mondayCheck.setChecked(false);
                    activityCircleBinding.tuesdayCheck.setChecked(false);
                    activityCircleBinding.wednesdayCheck.setChecked(false);
                    activityCircleBinding.thursdayCheck.setChecked(false);
                    activityCircleBinding.fridayCheck.setChecked(false);
                    activityCircleBinding.saturdayCheck.setChecked(false);
                }
            }
        });
        activityCircleBinding.saturdayCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (activityCircleBinding.onceCheck.isChecked()) {
                        activityCircleBinding.onceCheck.setChecked(false);
                    }
                }

            }
        });
        activityCircleBinding.sundayCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (activityCircleBinding.onceCheck.isChecked()) {
                        activityCircleBinding.onceCheck.setChecked(false);
                    }
                }
            }
        });
        activityCircleBinding.mondayCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (activityCircleBinding.onceCheck.isChecked()) {
                        activityCircleBinding.onceCheck.setChecked(false);
                    }
                }
            }
        });
        activityCircleBinding.tuesdayCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (activityCircleBinding.onceCheck.isChecked()) {
                        activityCircleBinding.onceCheck.setChecked(false);
                    }
                }
            }
        });
        activityCircleBinding.wednesdayCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (activityCircleBinding.onceCheck.isChecked()) {
                        activityCircleBinding.onceCheck.setChecked(false);
                    }
                }
            }
        });
        activityCircleBinding.thursdayCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (activityCircleBinding.onceCheck.isChecked()) {
                        activityCircleBinding.onceCheck.setChecked(false);
                    }
                }
            }
        });
        activityCircleBinding.fridayCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (activityCircleBinding.onceCheck.isChecked()) {
                        activityCircleBinding.onceCheck.setChecked(false);
                    }
                }
            }
        });
        activityCircleBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = "";
                int work = 0x00;
                if (activityCircleBinding.mondayCheck.isChecked()) {
                    result += getString(R.string.mon) + ",";
                    work = (work | 0x02);
                }
                if (activityCircleBinding.tuesdayCheck.isChecked()) {
                    result += getString(R.string.tue) + ",";
                    work = (work | 0x04);
                }
                if (activityCircleBinding.wednesdayCheck.isChecked()) {
                    result += getString(R.string.wed) + ",";
                    work = (work | 0x08);
                }
                if (activityCircleBinding.thursdayCheck.isChecked()) {
                    result += getString(R.string.thur) + ",";
                    work = (work | 0x10);
                }
                if (activityCircleBinding.fridayCheck.isChecked()) {
                    result += getString(R.string.fri) + ",";
                    work = (work | 0x20);
                }
                if (activityCircleBinding.saturdayCheck.isChecked()) {
                    result += getString(R.string.sat) + ",";
                    work = (work | 0x40);
                }
                if (activityCircleBinding.sundayCheck.isChecked()) {
                    result += getString(R.string.sun) + ",";
                    work = (work | 0x01);
                }
                if (activityCircleBinding.onceCheck.isChecked()) {
                    result = "";
                    result += getString(R.string.only_once);
                    work = 0x00;
                }
                if (result.contains(",")) {
                    result = result.substring(0, result.length() - 1);
                }
                if (TextUtils.isEmpty(result)) {
                    return;
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("circleTime", result);
                    intent.putExtra("circle", work);
                    setResult(CYCLE_CODE, intent);
                    finish();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
}
